package com.dingwei.lesyndrome.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.PersonalInfoBean;
import com.dingwei.lesyndrome.mvp.presenter.PersonalCenterPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.weight.TitleBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 个人中心
 */

public class PersonalCenterActivity extends BaseActivity implements Contract.PersonalCenterView {

    PersonalCenterPresenter personalCenterPresenter = new PersonalCenterPresenter(this);
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.ll_name)
    LinearLayout llName;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.my_collect)
    SuperTextView myCollect;
    @Bind(R.id.my_release)
    SuperTextView myRelease;
    @Bind(R.id.address)
    SuperTextView address;
    @Bind(R.id.share)
    SuperTextView share;
    @Bind(R.id.system_setup)
    SuperTextView systemSetup;
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.ll_personal_info)
    LinearLayout llPersonalInfo;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_personal_center;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseActivity.value.getIsLogin()){
            personalCenterPresenter.initData();
        }else {
            intent = new Intent(BaseActivity.context,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void initView() {
        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                intent = new Intent(BaseActivity.context, MyNewsActivity.class);
                startActivity(intent);
            }
        });

        if (TextUtils.isEmpty(BaseActivity.value.getUserNmae())){
            tvName.setText(BaseActivity.value.getPhone());
        }else {
            tvName.setText(BaseActivity.value.getUserNmae());
        }
        Glide.with(BaseActivity.context)
                .load(BaseActivity.value.getheadImg())
                .apply(MyApplication.headOptions)
                .into(imgHead);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_personal_info, R.id.my_collect,
            R.id.my_release, R.id.address, R.id.share,
            R.id.system_setup})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_personal_info:
                intent = new Intent(BaseActivity.context, PersonalInfoActivity.class);
                startActivityForResult(intent,0);
                break;
            case R.id.my_collect:
                intent = new Intent(BaseActivity.context, CollectActivity.class);
                startActivity(intent);
                break;
            case R.id.my_release:
                intent = new Intent(BaseActivity.context, MyReleaseActivity.class);
                startActivity(intent);
                break;
            case R.id.address:
                intent = new Intent(BaseActivity.context, AddressListActivity.class);
                startActivity(intent);
                break;
            case R.id.share:
                break;
            case R.id.system_setup:
                intent = new Intent(BaseActivity.context, SetupActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void setData(PersonalInfoBean bean) {
        if (TextUtils.isEmpty(bean.data.nickname)){
            tvName.setText(bean.data.phone);
        }else {
            tvName.setText(bean.data.nickname);
        }
        Glide.with(BaseActivity.context)
                .load(bean.data.showHportrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);

        BaseActivity.value.setUserName(bean.data.nickname);
        BaseActivity.value.setPhone(bean.data.phone);
        BaseActivity.value.setHeadImg(bean.data.showHportrait);
    }

    @Override
    public void onFail() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 5:
                intent = new Intent(BaseActivity.context,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
